<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Proveedor
 *
 * @author Jose Tejada
 */
class ProveedorDAO implements IFEntityDAO {

    private static $tbProveedor = NULL;
    private static $asAllFields = array( "*" );

    public static function traerPorId( $iId ) {
        $pb = NULL;
        if ( self::$tbProveedor == NULL ) {
            self::$tbProveedor = Tablas::getTbProveedor();
        }
//        $tb = new TbOfertaVigencia();
        try {
            if ( $iId != NULL && !empty( $iId ) ) {
                $result = ManejoBD::retornarRegistro(
                                self::$tbProveedor->getNombre()
                                , self::$asAllFields
                                , self::$tbProveedor->getCIdProveedor() . "=" . $iId . " AND activo=1"
                );
                if ( !is_null( $result ) && count( $result ) > 0 ) {

                    $pb = new ProveedorBean(
                                    $result[ 0 ][ self::$tbProveedor->getCIdProveedor() ]
                                    , $result[ 0 ][ self::$tbProveedor->getCRazonSocial() ]
                                    , $result[ 0 ][ self::$tbProveedor->getCRuc() ]
                                    , $result[ 0 ][ self::$tbProveedor->getCTelefono1() ]
                                    , $result[ 0 ][ self::$tbProveedor->getCTelefono2() ]
                                    , $result[ 0 ][ self::$tbProveedor->getCTelefono3() ]
                                    , $result[ 0 ][ self::$tbProveedor->getCDireccion() ]
                                    , $result[ 0 ][ self::$tbProveedor->getCContacto() ]
                                    , $result[ 0 ][ self::$tbProveedor->getCFechaRegistro() ]
                    );
                }
            }
        } catch ( Exception $e ) {
            error_log( $e->getMessage(), 3, "errores.log" );
            throw $e;
        }
        return $pb;
    }

    public static function traerTodos() {
        $result = NULL;
        $iNumRegistros = 0;
        $lstProveedorBean = Array( );
        if ( is_null( self::$tbProveedor ) ) {
            self::$tbProveedor = Tablas::getTbProveedor();
        }

        try {
            $result = ManejoBD::retornarRegistro(
                            self::$tbProveedor->getNombre(), self::$asAllFields, "activo=1" );
            if ( !is_null( $result ) && count( $result ) > 0 ) {
                $iNumRegistros = count( $result );
                for ( $i = 0; $i < $iNumRegistros; $i++ ) {
//                    $tb = new TbMarca();
                    $pb = new ProveedorBean(
                                    $result[ $i ][ self::$tbProveedor->getCIdProveedor() ]
                                    , $result[ $i ][ self::$tbProveedor->getCRazonSocial() ]
                                    , $result[ $i ][ self::$tbProveedor->getCRuc() ]
                                    , $result[ $i ][ self::$tbProveedor->getCTelefono1() ]
                                    , $result[ $i ][ self::$tbProveedor->getCTelefono2() ]
                                    , $result[ $i ][ self::$tbProveedor->getCTelefono3() ]
                                    , $result[ $i ][ self::$tbProveedor->getCDireccion() ]
                                    , $result[ $i ][ self::$tbProveedor->getCContacto() ]
                                    , $result[ $i ][ self::$tbProveedor->getCFechaRegistro() ]
                    );
                    $lstProveedorBean[ $i ] = $pb;
                }
            }
        } catch ( Exception $e ) {
            error_log( $e->getMessage(), 3, "errores.log" );
            throw $e;
        }
        return $lstProveedorBean;
    }

    public static function insertar( EntityBean $proveedorBean ) {
        $blResp = FALSE;
        if ( is_null( self::$tbProveedor ) ) {
            self::$tbProveedor = Tablas::getTbProveedor();
        }

        try {
            if ( !is_null( $proveedorBean ) ) {
//            $t = new TbMarca();
                $blResp = ManejoBD::insertarDatos(
                                self::$tbProveedor->getNombre()
                                , self::$tbProveedor->getTodosCampos()
                                , $proveedorBean->getTodosValores()
                );
            }
        } catch ( Exception $e ) {
            error_log( $e->getMessage(), 3, "errores.log" );
            throw $e;
        }
        return $blResp;
    }

    public static function actualizar( EntityBean $proveedorBean, $sCondicion ) {
        $blResp = FALSE;
        if ( is_null( self::$tbProveedor ) ) {
            self::$tbProveedor = Tablas::getTbProveedor();
        }

        try {
            if ( !is_null( $proveedorBean ) ) {
//            $t = new TbOferta();
                $blResp = ManejoBD::actualizarDatos(
                                self::$tbProveedor->getNombre()
                                , self::$tbProveedor->getTodosCampos()
                                , $proveedorBean->getTodosValores()
                                , $sCondicion
                );
            }
        } catch ( Exception $e ) {
            error_log( $e->getMessage(), 3, "errores.log" );
            throw $e;
        }
        return $blResp;
    }

    public static function eliminarPorId( EntityBean $proveedorBean ) {
        $blResp = FALSE;
        if ( is_null( self::$tbProveedor ) ) {
            self::$tbProveedor = Tablas::getTbProveedor();
        }

        try {
            if ( !is_null( $proveedorBean ) ) {
//            $t = new TbOferta();
                $blResp = ManejoBD::eliminarRegistro(
                                self::$tbProveedor->getNombre()
                                , self::$tbProveedor->getCIdProveedor() . "=" . $proveedorBean->getIdProveedor() );
            }
        } catch ( Exception $e ) {
            error_log( $e->getMessage(), 3, "errores.log" );
            throw $e;
        }
        return $blResp;
    }

}

?>
